import random
import time
from decimal import Decimal
class User:
    def __init__(self,name,total,num):
        self.name = name
        self.total = total
        self.__num = num
        self.red_envelops = []#此属性用于用户发红包时生成红包列表
        self.get_money = 0#用于记录用户抢到的红包金额
    def get_red_envelops(self,get_money):
            self.get_money += get_money
    def Generating_red_envelopes(self,payment):#用于用户发红包时，随机生成红包列表并返回
            for i in range(1,5):
                if i ==4:
                    self.red_envelops.append(round(payment,2))#最后一个红包的金额是payment中剩余的金额。
                    break
                hb = round(random.uniform(0.01,payment),2)
                payment -= hb
                self.red_envelops.append(hb)
            return self.red_envelops


def Grab_red_envelope(s):#抢红包函数。
    users = [user1,user2,user3,user4,user5]
    count = 4
    n = 0
    print("用户正在抢红包中...")
    time.sleep(1)
    print("抢红包结果如下:")
    time.sleep(1)
    while n < 4:
        a = random.randint(0,count)
        user = users[a]
        user.get_red_envelops(s[n])
        print("{0}抢到了{1}元红包".format(user.name,s[n]))
        users.pop(a)
        if s[n]==max(s):
            lucky_dog = user.name
        elif s[n] == min(s):
            unlucky_dog = user.name
        count -= 1
        n += 1
    time.sleep(1)
    print("本次抢红包的幸运王是{0}，倒霉王是{1}。很遗憾，{2}本次没有抢到红包".format(lucky_dog,unlucky_dog,users[0].name))
print("正在进入程序中，请稍候....")
time.sleep(3)
print("欢迎使用红包系统")
time.sleep(1)
user1 = User("包乐尔夫",50,4)
user2 = User("胡康成",40,4)
user3 = User("林子琦",45,4)
user4 = User("耿雯君",55,4)
user5 = User("韩寒",70,4)
users = [user1,user2,user3,user4,user5]

for i in users:
    print("当前用户为：{0}".format(i.name))
    time.sleep(1)
    payment = int(input("您的余额为{0},请输入发红包的总金额:".format(i.total)))
    while payment > i.total or payment <= 0:
        if payment > i.total:
            payment = int(input("抱歉，您的余额不足，请您重新输入发红包的金额："))
        else:
            payment = int(input("错误输入！请您输入0~{0}的红包金额:".format(i.total)))
    s=i.Generating_red_envelopes(payment)
    Grab_red_envelope(s)
getmoney = [user1.get_money,user2.get_money,user3.get_money,user4.get_money,user5.get_money]
for i in users:
    if i.get_money == max(getmoney):
        print("最终的幸运王:{0}".format(i.name))
    elif i.get_money == min(getmoney):
        print("最终的倒霉王是:{0}".format(i.name))

input("输入回车结束程序......")
time.sleep(2)
